package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Department;
import cn.wolfcode.car.business.domain.Employee;
import cn.wolfcode.car.business.query.EmployeeQueryObject;
import cn.wolfcode.car.business.service.IDepartmentService;
import cn.wolfcode.car.business.service.IEmployeeService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 控制器
 * @author RNIZ
 */
@Controller
@RequestMapping("business/employee")
public class EmployeeController {
    /**模板前缀*/
    private static final String PREFIX = "business/employee/";

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    /**页面
    列表*/
    @RequiresPermissions("business:employee:view")
    @RequestMapping("/listPage")
    public String listPage(Model model){
        List<Department> dept = departmentService.selectAll();
        model.addAttribute("dept",dept);
        return PREFIX + "list";
    }

    @RequiresPermissions("business:employee:add")
    @RequestMapping("/addPage")
    public String addPage(Model model){
        List<Department> dept = departmentService.selectAll();
        model.addAttribute("dept",dept);
        return PREFIX + "add";
    }


    @RequiresPermissions("business:employee:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("employee", employeeService.get(id));
        List<Department> dept = departmentService.selectAll();
        model.addAttribute("dept",dept);
        return PREFIX + "edit";
    }

    /**数据
     列表*/
    @RequiresPermissions("business:employee:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Employee> query(EmployeeQueryObject qo){
        return employeeService.query(qo);
    }

    /**
     * 新增
     * @param employee
     * @return
     */
    @RequiresPermissions("business:employee:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Employee employee){
        employeeService.insert(employee);
        return AjaxResult.success();
    }

    /**
     * 编辑
     *
     * @param employee 服务项目
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:employee:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Employee employee){
        employeeService.update(employee);
        return AjaxResult.success();
    }

    /**
     * 删除
     *
     * @param
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:employee:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long ids){
        employeeService.delete(ids);
        return AjaxResult.success();
    }
    @RequestMapping("/queryByDepartment")
    public String queryByDepartment(Long id,Model model){
        System.out.println(id);
        Department dept = departmentService.queryByDepartment(id);
        model.addAttribute("dept",dept);
        return PREFIX + "query";
    }
}
